package com.qf.flinketl.service;

import com.alibaba.fastjson2.JSONObject;
import com.qf.flinketl.EventId;
import com.qf.flinketl.protobuf.MessageRowOuterClass;
import com.qf.flinketl.vo.UserRelationVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: @Author: KyrieFc @Date: 2023/10/24
 */
@Service("exposureService")
public class ExposureService extends AbstractMetaService {
  private static final long serialVersionUID = 1L;
  private static final Logger logger = LoggerFactory.getLogger(ExposureService.class);

  /**
   * 解析数据
   *
   * @param key
   * @param msg
   * @param dataName
   * @return
   * @throws Exception
   */
  @Override
  public Map<String, Object> parse(String key, String msg, String dataName) throws Exception {
    logger.debug("parse msg:{}", msg);
    JSONObject jsonObject = JSONObject.parseObject(msg);
    if (isValidMsg(jsonObject)) return null;
    String time = jsonObject.getString("time");
    String pin = jsonObject.getString("pin");
    EventId eventId = getEventId(jsonObject.getString("eventId"));
    if (eventId.equals(EventId.UNKNOWN_EVENT_ID)) {
      logger.error("unknown eventId:{}", eventId);
      return null;
    }
    Map<String, Object> parsedMap = new HashMap<>(8);
    parsedMap.put("time", time);
    parsedMap.put("pin", pin);
    parsedMap.put("eventId", eventId);
    return parsedMap;
  }

  /**
   * 映射数据
   *
   * @param parsedMap
   * @return
   * @throws Exception
   */
  @Override
  public List<MessageRowOuterClass> map(Map<String, Object> parsedMap) throws Exception {
    if (parsedMap == null) {
      return null;
    }
    List<MessageRowOuterClass> msgRow = new ArrayList<>();
    return msgRow;
  }

  @Override
  public void saveValues(MessageRowOuterClass messageRowOuterClass) throws Exception {}

  @Override
  public Map<String, Object> filter(Map<String, Object> parsedMap) throws Exception {
    return null;
  }

  @Override
  public UserRelationVO getUserRelationVo(MessageRowOuterClass msgRow) throws Exception {
    return null;
  }

  @Override
  public boolean isValidMsg(JSONObject json) {
    return json.containsKey("pin") && json.containsKey("exposureId");
  }

  private EventId getEventId(String eventId) {
    return EventId.valueOf(eventId);
  }
}
